package com.springboot.admin.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/************************************************************************
 *
 *    Copyright  Copyright 2021 Google Inc.
 *    File Name: RedisUrlCountInterceptor
 *    Description: redis拦截器
 *
 *    Version: V1.0
 *    Author: Fuc
 *    Create Time: 2021-01-10
 *
 *************************************************************************/

@Component
public class RedisUrlCountInterceptor implements HandlerInterceptor {

  @Autowired StringRedisTemplate redisTemplate;

  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
      throws Exception {
    String uri = request.getRequestURI();

    // 默认每次访问当前uri就会计数+1
    redisTemplate.opsForValue().increment(uri);

    return true;
  }
}
